package com.leadership.api.job;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.result.HttpResult;
import com.leadership.api.service.IdmService;

/**
 * 
 * @author dan
 *
 */
@Component("idmJob")
public class IdmJob {

    private static final Logger logger = LoggerFactory.getLogger(IdmJob.class);

    @Autowired
    private IdmService idmService;

    @Value("#{config['api.host']}")
    private String apiHost;

    /**
     * run at 01:00 am every day
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void scanIdmOrgStart() {

        logger.info("#scanIdmOrgStart() - start at {}", new Date());

        try {

            HttpResult result = idmService.updateOrg();
            if (result.isValid()) {
                logger.info("#scanIdmOrgStart() - create idmOrg success");
            } else {
                logger.error("#scanIdmOrgStart() - create idmOrg failed");
            }

        } catch (Exception e) {
            logger.error("#scanIdmOrgStart() - occur exception=[{}]", e);
        }

        logger.info("#scanIdmOrgStart() - finish at {}", new Date());

    }

    /**
     * 每天15:00
     */
//    @Scheduled(cron = "0 0 15 * * ?")
//    public void scanIdmUserStart() {
//
//        logger.info("#scanIdmUserStart() - start at {}", new Date());
//
//        try {
//
//            HttpResult result = idmService.updateAllUser();
//            if (result.isValid()) {
//                logger.info("#scanIdmUserStart() - create idmOrg success");
//            } else {
//                logger.error("#scanIdmUserStart() - create idmOrg failed");
//            }
//
//        } catch (Exception e) {
//            logger.error("#scanIdmUserStart() - occur exception=[{}]", e);
//        }
//
//        logger.info("#scanIdmUserStart() - finish at {}", new Date());
//
//    }

    /**
     * 每天01:50
     */
    @Scheduled(cron = "0 50 1 * * ?")
    public void scanIdmUserStart2() {

        logger.info("#scanIdmUserStart2() - start at {}", new Date());

        try {

            HttpResult result = idmService.updateAllUser();
            if (result.isValid()) {
                logger.info("#scanIdmUserStart2() - create idmOrg success");
            } else {
                logger.error("#scanIdmUserStart2() - create idmOrg failed");
            }

        } catch (Exception e) {
            logger.error("#scanIdmUserStart2() - occur exception=[{}]", e);
        }

        logger.info("#scanIdmUserStart2() - finish at {}", new Date());

    }

    /**
     * 每30分钟
     */
    @Scheduled(cron = "0 0/30 * * * ? ")
    public void scanLearningMapStudent() {

        logger.info("#scanLearningMapStudent() - start at {}", new Date());

        try {

            HttpResult result = idmService.noticeLearningMap();
            if (result.isValid()) {
                logger.info("#scanLearningMapStudent() - create idmOrg success");
            } else {
                logger.error("#scanLearningMapStudent() - create idmOrg failed");
            }

        } catch (Exception e) {
            logger.error("#scanLearningMapStudent() - occur exception=[{}]", e);
        }

        logger.info("#scanLearningMapStudent() - finish at {}", new Date());

    }

    @Scheduled(cron = "0 0 2 * * ?")
    public void statLearningMapStudent() {

        logger.info("#scanLearningMapStudent() - start at {}", new Date());

        try {

            HttpResult result = idmService.statLearningMap();
            if (result.isValid()) {
                logger.info("#statLearningMapStudent() - stat learningmap success");
            } else {
                logger.error("#statLearningMapStudent() - stat learningmap failed");
            }

        } catch (Exception e) {
            logger.error("#statLearningMapStudent() - occur exception=[{}]", e);
        }

        logger.info("#statLearningMapStudent() - finish at {}", new Date());

    }

}
